package cn.sinata.wanglin.property.activity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.JsonUtils;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.property.view.RoundProgressBar;

/**
 * 物业---表扬批评
 */
public class PraiseAndCriticizeActivity extends TitleActivity {

    private String id;
    @BindView(R.id.rpb_score)
    RoundProgressBar rpb_score;
    @BindView(R.id.tv_praise)
    TextView tv_praise;
    @BindView(R.id.tv_criticize)
    TextView tv_criticize;
    @BindView(R.id.tv_wanttozan)
    RadioButton rbtn_praise;
    @BindView(R.id.tv_wanttocai)
    RadioButton rbtn_criticize;
    @BindView(R.id.et_content)
    EditText et_content;
    private int evaluateType = -1;//评价类型（0，赞；1，踩）
    private String userId;

    @Override
    protected int setContentId() {
        return R.layout.activity_praiseandcriticize;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("表扬批评");
        addRightButton("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (evaluateType == -1) {
                    showToast("请选择赞或踩！");
                    return;
                }
                String content = et_content.getText().toString().trim();
                showDialog();
                addPraiseOrCriticize(content);
            }
        });

        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        id = getIntent().getStringExtra("id");

        showDialog();
        getData();
    }

    @OnCheckedChanged({R.id.tv_wanttozan,R.id.tv_wanttocai})
    void OnCheckedChange(CompoundButton btn,boolean isChecked){
        if (isChecked) {
            if (btn.getId() == R.id.tv_wanttozan) {
                rbtn_criticize.setChecked(false);
                evaluateType = 0;
            } else {
                rbtn_praise.setChecked(false);
                evaluateType = 1;
            }
        }
    }

    void getData(){
        HttpManager.getPraiseOrCriticize(id)
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        JsonUtils jsonUtils = new JsonUtils(jsonObject);
                        String praise = jsonUtils.optString("praise");
                        String criticize = jsonUtils.optString("criticize");
                        tv_praise.setText(praise);
                        tv_criticize.setText(criticize);
                        getProgress(praise);
                    }
                });
    }

    void getProgress(String praise) {
        String p = praise.replace("%","");
        try {
            int progress = Integer.parseInt(p);
            rpb_score.setProgress(progress);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

    }

    void addPraiseOrCriticize(String content){
        HttpManager.addPraiseOrCriticize(id,userId,evaluateType,content)
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
//                        et_content.setText("");
//                        getData();
                        finish();
                    }
                });
    }
}
